package pers.zxk.jlytnews.servlet;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import pers.zxk.jlytnews.dao.NewsDao;
import pers.zxk.jlytnews.dao.WxArticleDao;
import pers.zxk.jlytnews.dao.WxChannelDao;
import pers.zxk.jlytnews.dao.impl.NewsDaoImpl;
import pers.zxk.jlytnews.dao.impl.WxArticleDaoImpl;
import pers.zxk.jlytnews.dao.impl.WxChannelDaoImpl;
import pers.zxk.jlytnews.entity.*;
import pers.zxk.jlytnews.manager.UserStateManager;
import pers.zxk.jlytnews.util.CommonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.List;


@MultipartConfig
public class NewsServlet extends HttpServlet {
    private NewsDao mNewsDao = new NewsDaoImpl();
    private WxArticleDao mWxArticleDao = new WxArticleDaoImpl();
    private WxChannelDao mWxChannelDao = new WxChannelDaoImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取能够与“url-pattern”中匹配的路径
        String method = req.getServletPath();

        //(此时处理的请求是查询 query.do)
        System.out.println("request.getServletPath()获取的值为: " + method);//输出 /query.do

        //2.通过字符串截取，把方法名 query 截取出来
        int i = method.lastIndexOf("/");
        method = method.substring(i + 1, method.length());

        System.out.println("截取后的值为： " + method);

        Method m = null;
        try {
            //3.获取当前类中名字为 method 的方法
            m = this.getClass().getDeclaredMethod(method,
                    HttpServletRequest.class, HttpServletResponse.class);

            //4.调用 method 方法
            m.setAccessible(true);
            m.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void addNews(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String type = "2";//类型
        String title = req.getParameter("title");//标题
        Part iconPart = req.getPart("icon");//标题图片
        String iconPath = CommonUtil.saveImage(iconPart);//保存图片，返回图片url
        String url = "";//新闻链接（转发）
        String contents = req.getParameter("contents");//新闻内容（原创）
        String date = req.getParameter("date");//日期
        String source = req.getParameter("source");//来源名称

        try {
            boolean isSuccess = mNewsDao.addNews(Integer.parseInt(type), title, iconPath, url, contents, date, source);
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "添加成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "添加失败", 1, "");
            }
        } catch (Exception e) {
            //CommonUtil.sendJsonData(resp, "添加失败", 1, "sql数据库增删改异常");
            CommonUtil.sendJsonData(resp, "添加失败", 1, e.getMessage());
        }
    }

    public void addNewsLink(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String type = "1";//类型
        String title = req.getParameter("title");//标题
        String iconPath = req.getParameter("icon");//图片地址
        String url = req.getParameter("url");//新闻链接（转发）
        String contents = "";//新闻内容（原创）
        String date = req.getParameter("date");//日期
        String source = req.getParameter("source");//来源名称

        try {
            boolean isSuccess = mNewsDao.addNews(Integer.parseInt(type), title, iconPath, url, contents, date, source);
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "添加成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 1, "未知错误，请联系管理员！");
            }
        } catch (Exception e) {
            //CommonUtil.sendJsonData(resp, "添加失败", 1, "sql数据库增删改异常");
            CommonUtil.sendJsonData(resp, null, 1, e.getMessage());
        }
    }

    public void delNews(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String id = req.getParameter("id");

        try {
            //删除图片
            News news = mNewsDao.getNews(Integer.parseInt(id));
            String url = news.getIcon();
            String realPath = CommonUtil.urlToPath(url);
            new File(realPath).delete();

            //删除记录
            boolean isSuccess = mNewsDao.delNews(Integer.parseInt(id));
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "删除成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "删除失败", 1, "");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, "删除失败", 1, ex.getMessage());
        }


    }

    public void updateNews(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String id = req.getParameter("id");
        String type = req.getParameter("type");//类型
        String title = req.getParameter("title");//标题
        Part part = req.getPart("icon");
        String iconUrl = CommonUtil.saveImage(part);//保存图片，返回图片路径
        String url = req.getParameter("url");
        String contents = req.getParameter("contents");
        String date = req.getParameter("date");
        String source = req.getParameter("source");

        try {
            //删除原图片
            News news = mNewsDao.getNews(Integer.parseInt(id));
            String oldIconUrl = news.getIcon();
            String realPath = CommonUtil.urlToPath(oldIconUrl);
            if (!oldIconUrl.equals(iconUrl)) {
                new File(realPath).delete();
            }

            boolean isUpdated = mNewsDao.updateNews(Integer.parseInt(id), Integer.parseInt(type),
                    title, iconUrl, url, contents, date, source);
            if (isUpdated) {
                CommonUtil.sendJsonData(resp, "更新成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "更新失败", 1, "");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, "更新失败", 1, ex.getMessage());
        }

    }

    public void getNews(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String id = req.getParameter("id");

        try {
            News news = mNewsDao.getNews(Integer.parseInt(id));
            if (news != null) {
                CommonUtil.sendJsonData(resp, news, 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 0, "信息获取失败");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, null, 1, ex.getMessage());
        }

    }

    public void getNewsList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String size = req.getParameter("size");

        if (page == null) {
            page = "0";
        }
        if (size == null) {
            size = "0";
        }

        try {
            List<News> list = mNewsDao.getNewsList(Integer.valueOf(page), Integer.valueOf(size));
            JSONArray jsonArray = JSONArray.fromObject(list);
            if (jsonArray != null) {
                CommonUtil.sendJsonData(resp, jsonArray, 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 1, "未找到数据");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, null, 1, ex.getMessage());
        }
    }

    //↓↓微信文章
    public void addWxArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String aid = req.getParameter(WxArticleDaoImpl.COL_AID);
        String appmsgid = req.getParameter(WxArticleDaoImpl.COL_APPMSGID);
        String itemIdx = req.getParameter(WxArticleDaoImpl.COL_ITEMIDX);
        String cover = req.getParameter(WxArticleDaoImpl.COL_COVER);
        String link = req.getParameter(WxArticleDaoImpl.COL_LINK);
        String title = req.getParameter(WxArticleDaoImpl.COL_TITLE);
        String createTime = req.getParameter(WxArticleDaoImpl.COL_CREATE_TIME);
        String updateTime = req.getParameter(WxArticleDaoImpl.COL_UPDATE_TIME);
        String mediaDuration = req.getParameter(WxArticleDaoImpl.COL_MEDIA_DURATION);
        String mediaStatus = req.getParameter(WxArticleDaoImpl.COL_MEDIAAPI_PUBLISH_STATUS);
        String fakeId = req.getParameter(WxArticleDaoImpl.COL_FAKE_ID);

        try {
            boolean isSuccess = mWxArticleDao.addWxArticle(aid, Long.parseLong(appmsgid), Integer.parseInt(itemIdx),
                    cover, link, title, Integer.parseInt(createTime), Integer.parseInt(updateTime), mediaDuration,
                    Integer.parseInt(mediaStatus), fakeId);
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "添加成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 1, "添加失败");
            }
        } catch (Exception e) {
            //CommonUtil.sendJsonData(resp, "添加失败", 1, "sql数据库增删改异常");
            CommonUtil.sendJsonData(resp, "添加失败", 1, e.getMessage());
        }
    }

    public void addWxArticles(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        //获取Request传递（POST）过来的数据
        String jsonData = req.getParameter("data");//获取参数的数据
        JSONObject jsonObject = JSONObject.fromObject(jsonData);//将数据转换成jsonobject
        Object data = jsonObject.get("articles");//提取jsonobject中的key为foodmenus的数据
        JSONArray jsonArray = JSONArray.fromObject(data);//将数据转换成jsonarray

        boolean isSuccess = false;
        try {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                String aid = object.getString(WxArticleDaoImpl.COL_AID);
                String appmsgid = object.getString(WxArticleDaoImpl.COL_APPMSGID);
                String itemIdx = object.getString(WxArticleDaoImpl.COL_ITEMIDX);
                String cover = object.getString(WxArticleDaoImpl.COL_COVER);
                String link = object.getString(WxArticleDaoImpl.COL_LINK);
                String title = object.getString(WxArticleDaoImpl.COL_TITLE);
                String createTime = object.getString(WxArticleDaoImpl.COL_CREATE_TIME);
                String updateTime = object.getString(WxArticleDaoImpl.COL_UPDATE_TIME);
                String mediaDuration = object.getString(WxArticleDaoImpl.COL_MEDIA_DURATION);
                String mediaStatus = object.getString(WxArticleDaoImpl.COL_MEDIAAPI_PUBLISH_STATUS);
                String fakeId = object.getString(WxArticleDaoImpl.COL_FAKE_ID);

                isSuccess = mWxArticleDao.addWxArticle(aid, Long.parseLong(appmsgid), Integer.parseInt(itemIdx),
                        cover, link, title, Integer.parseInt(createTime), Integer.parseInt(updateTime), mediaDuration,
                        Integer.parseInt(mediaStatus), fakeId);
            }
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "添加成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "添加失败", 1, "");
            }
        } catch (Exception exception) {
            CommonUtil.sendJsonData(resp, "添加失败", 1, exception.getMessage());
        }
    }

    public void delWxArticle(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String id = req.getParameter("id");

        try {
            //删除记录
            boolean isSuccess = mWxArticleDao.delWxArticle(Integer.parseInt(id));
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "删除成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "删除失败", 1, "");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, "删除失败", 1, ex.getMessage());
        }


    }

    public void updateWxArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String id = req.getParameter(WxArticleDaoImpl.COL_ID);
        String aid = req.getParameter(WxArticleDaoImpl.COL_AID);
        String appmsgid = req.getParameter(WxArticleDaoImpl.COL_APPMSGID);
        String itemIdx = req.getParameter(WxArticleDaoImpl.COL_ITEMIDX);
        String cover = req.getParameter(WxArticleDaoImpl.COL_COVER);
        String link = req.getParameter(WxArticleDaoImpl.COL_LINK);
        String title = req.getParameter(WxArticleDaoImpl.COL_TITLE);
        String createTime = req.getParameter(WxArticleDaoImpl.COL_CREATE_TIME);
        String updateTime = req.getParameter(WxArticleDaoImpl.COL_UPDATE_TIME);
        String mediaDuration = req.getParameter(WxArticleDaoImpl.COL_MEDIA_DURATION);
        String mediaStatus = req.getParameter(WxArticleDaoImpl.COL_MEDIAAPI_PUBLISH_STATUS);
        String fakeId = req.getParameter(WxArticleDaoImpl.COL_FAKE_ID);

        try {
            boolean isSuccess = mWxArticleDao.updateWxArticle(Integer.parseInt(id), aid, Long.parseLong(appmsgid), Integer.parseInt(itemIdx),
                    cover, link, title, Integer.parseInt(createTime), Integer.parseInt(updateTime), mediaDuration,
                    Integer.parseInt(mediaStatus), fakeId);
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "更新成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "更新失败", 1, "");
            }
        } catch (Exception e) {
            //CommonUtil.sendJsonData(resp, "添加失败", 1, "sql数据库增删改异常");
            CommonUtil.sendJsonData(resp, "更新失败", 1, e.getMessage());
        }
    }

    public void getWxArticleList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String size = req.getParameter("size");
        String fakeId = req.getParameter("fakeid");

        page = page != null ? page : "0";
        size = size != null ? size : "0";

        try {
            List<WxArticleToClient> list = null;
            if (fakeId == null) {
                list = mWxArticleDao.getWxArticleList(Integer.valueOf(page), Integer.valueOf(size));
            } else {
                list = mWxArticleDao.getWxArticleList(Integer.valueOf(page), Integer.valueOf(size), fakeId);
            }
            JSONArray jsonArray = JSONArray.fromObject(list);
            if (jsonArray != null) {
                CommonUtil.sendJsonData(resp, jsonArray, 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 1, "未找到数据");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, null, 1, ex.getMessage());
        }
    }

    @Deprecated
    public void getWxArticleTinyList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String fakeId = req.getParameter("fakeid");

        if (fakeId == null) {
            fakeId = "";
        }
        try {
            List<WxArticleTiny> list = mWxArticleDao.getWxArticleTinyList();
            JSONArray jsonArray = JSONArray.fromObject(list);
            if (jsonArray != null) {
                CommonUtil.sendJsonData(resp, jsonArray, 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 1, "未找到数据");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, null, 1, ex.getMessage());
        }
    }

    public void getAidList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String fakeId = req.getParameter("fakeid");
        String startTime = req.getParameter("starttime");
        String endTime = req.getParameter("endtime");

        int sTime = startTime != null ? Integer.parseInt(startTime) : 0;
        int eTime = endTime != null ? Integer.parseInt(endTime) : 0;
        try {
            List<String> list = mWxArticleDao.getAidList(fakeId, sTime, eTime);
            JSONArray jsonArray = JSONArray.fromObject(list);
            if (jsonArray != null) {
                CommonUtil.sendJsonData(resp, jsonArray, 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 1, "未找到数据");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, null, 1, ex.getMessage());
        }
    }

    //↓↓微信公众号
    public void addWxChannel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String title = req.getParameter(WxChannelDaoImpl.COL_TITLE);
        String fakeId = req.getParameter(WxChannelDaoImpl.COL_FAKE_ID);
        String nickName = req.getParameter(WxChannelDaoImpl.COL_NICKNAME);
        String headImg = req.getParameter(WxChannelDaoImpl.COL_ROUND_HEAD_IMG);

        try {
            boolean isSuccess = mWxChannelDao.addWxChannel(title, fakeId, nickName, headImg);
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "添加成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "添加失败", 1, "添加失败");
            }
        } catch (Exception e) {
            //CommonUtil.sendJsonData(resp, "添加失败", 1, "sql数据库增删改异常");
            CommonUtil.sendJsonData(resp, "添加失败", 1, e.getMessage());
        }
    }

    public void delWxChannel(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String id = req.getParameter("id");

        try {
            //删除记录
            boolean isSuccess = mWxChannelDao.delWxChannel(Integer.parseInt(id));
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "删除成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "删除失败", 1, "");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, "删除失败", 1, ex.getMessage());
        }


    }

    public void updateWxChannel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String id = req.getParameter(WxChannelDaoImpl.COL_ID);
        String title = req.getParameter(WxChannelDaoImpl.COL_TITLE);
        String fakeId = req.getParameter(WxChannelDaoImpl.COL_FAKE_ID);
        String nickName = req.getParameter(WxChannelDaoImpl.COL_NICKNAME);
        String headImg = req.getParameter(WxChannelDaoImpl.COL_ROUND_HEAD_IMG);

        try {
            boolean isSuccess = mWxChannelDao.updateWxChannel(Integer.parseInt(id), title, fakeId, nickName, headImg);
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "更新成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "更新失败", 1, "");
            }
        } catch (Exception e) {
            //CommonUtil.sendJsonData(resp, "添加失败", 1, "sql数据库增删改异常");
            CommonUtil.sendJsonData(resp, "更新失败", 1, e.getMessage());
        }
    }

    public void getWxChannel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");

        try {
            WxChannel wxChannel = mWxChannelDao.getWxChannel(Integer.parseInt(id));
            if (wxChannel != null) {
                CommonUtil.sendJsonData(resp, wxChannel, 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 0, "信息获取失败");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, null, 1, ex.getMessage());
        }
    }

    public void getWxChannelList(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        try {
            List<WxChannel> list = mWxChannelDao.getWxChannelList();
            JSONArray jsonArray = JSONArray.fromObject(list);
            if (jsonArray != null) {
                CommonUtil.sendJsonData(resp, jsonArray, 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 1, "未找到数据");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, null, 1, ex.getMessage());
        }
    }

    public void setWxChannelSort(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        String id = req.getParameter("id");
        String sort = req.getParameter("sort");
        try {
            boolean isSuccess = mWxChannelDao.setSort(Integer.parseInt(id), Integer.parseInt(sort));
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "修改成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, "修改失败", 1, "");
            }
        } catch (Exception e) {
            //CommonUtil.sendJsonData(resp, "修改失败", 1, "sql数据库增删改异常");
            CommonUtil.sendJsonData(resp, "修改失败", 1, e.getMessage());
        }
    }

    //↓↓微信公众号登录状态

    public void updateWxAccount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        // 对请求包设置编码
        req.setCharacterEncoding("utf-8");

        String token = req.getParameter("token");
        String cookie = req.getParameter("cookie");

        try {
            boolean isSuccess = mWxChannelDao.updateWxAccount(token, cookie);
            if (isSuccess) {
                CommonUtil.sendJsonData(resp, "更新成功", 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 1, "更新失败");
            }
        } catch (Exception e) {
            //CommonUtil.sendJsonData(resp, "添加失败", 1, "sql数据库增删改异常");
            CommonUtil.sendJsonData(resp, null, 1, e.getMessage());
        }
    }

    public void getWxAccount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //判断用户权限
        if (!UserStateManager.isAdmin(req)) {
            CommonUtil.sendJsonData(resp, null, 1, "无权操作！");
            return;
        }

        try {
            WxAccountInfo wxAccount = mWxChannelDao.getWxAccount();
            if (wxAccount != null) {
                CommonUtil.sendJsonData(resp, wxAccount, 0, "");
            } else {
                CommonUtil.sendJsonData(resp, null, 0, "信息获取失败");
            }
        } catch (Exception ex) {
            CommonUtil.sendJsonData(resp, null, 1, ex.getMessage());
        }
    }
}
